/*
 * Created on Feb 3, 2005
 *
 * 
 */
package weather.net;

import java.io.DataInputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author Brian
 * 
 */
public class Temperature {
    String curTemp;
    static Proxy proxy;
    
    private static Temperature _instance = null;

    public static Temperature instance() {
        if (_instance == null) {
            _instance = new Temperature();
            proxy = new Proxy();
            _instance.updateTemperature();
        }
        return _instance;
    }

    private Temperature() {
    }

    public String getTemperature() {
        return curTemp;
    }

    public void updateTemperature() {
        String temperature = "0";
        URL url;
        URLConnection c;
      
        try {
            url = new URL("http://www.atd.ucar.edu/cgi-bin/weather.cgi?site=fl&units=english");
            
            if (proxy.useProxy()) {
                proxy.enableProxy();
            }
            c = url.openConnection();
            DataInputStream dis = new DataInputStream(c.getInputStream());
            String line = "";
            while (line.indexOf("<th>Temperature</th>") == -1) {
                line = dis.readLine();
            }
            line = dis.readLine();
            String[] tokens = line.split(new String(">"));
            temperature = tokens[1].substring(0, 2);
            if (temperature.endsWith(".")) {
                temperature = tokens[1].substring(0, 1);
            }

            dis.close();
           
        } catch (Exception e) {
            e.printStackTrace();
        }

        curTemp = temperature;
    }
}
